package com.learn.disruptor.basic;

import com.learn.disruptor.basic.event.LongEvent;
import com.learn.disruptor.basic.handler.LongEventHandler;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;

/**
 * @author XuYang
 * @since 2025/2/21 16:36
 */
public class BasicDisruptor {

    public static Disruptor<LongEvent> createAndStart(int bufferSize, LongEventHandler handler) {

        Disruptor<LongEvent> disruptor = new Disruptor<>(
            // 事件工厂
            LongEvent::new
            // ringBuffer大小(定义这个圆的元素块数量)
            , bufferSize
            // 线程工厂
            , DaemonThreadFactory.INSTANCE
            // 生产者类型   SINGLE(单生产者)  MULTI(多生产者)
            , ProducerType.SINGLE
            // 等待策略
            , new BlockingWaitStrategy()
        );

        // 配置事件处理器
        disruptor.handleEventsWith(handler);

        // 启动
        disruptor.start();
        return disruptor;
    }
}
